package com.simple.chiralium.upms.biz.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.simple.chiralium.common.core.annotion.operationLogger.OperationLogger;
import com.simple.chiralium.common.core.util.Result;
import com.simple.chiralium.upms.api.dto.UserInfo;
import com.simple.chiralium.upms.api.entity.SysUser;
import com.simple.chiralium.upms.biz.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统用户controller
 *
 * @author SimpleZzz
 * @date 2021/12/05
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class SysUserController {

    /**
     * 系统用户service
     */
    private final SysUserService sysUserService;

    /**
     * 获取所有用户信息
     *
     * @return {@link Result}<{@link List}<{@link SysUser}>>
     */
    @GetMapping
    @OperationLogger
    public Result<List<SysUser>> findAll() {
        return Result.ok(sysUserService.list());
    }

    /**
     * 根据ID查询用户信息
     *
     * @param id id
     * @return {@link Result}<{@link SysUser}>
     */
    @GetMapping("/{id}")
    public Result<SysUser> findById(@PathVariable String id) {
        return Result.ok(sysUserService.getById(id));
    }

    @GetMapping("/info/{username}")
    public Result<UserInfo> info(@PathVariable String username){
        SysUser sysUser = sysUserService.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, username));
        if (sysUser==null){
            return Result.failed(String.format("未查询到该用户信息 %s",username));
        }
        return Result.ok(sysUserService.getUserInfo(sysUser));
    }

    /**
     * 新增用户
     *
     * @param user 用户
     * @return {@link Result}<{@link SysUser}>
     */
    @PostMapping
    public Result<String> add(@RequestBody SysUser user) {
        return sysUserService.addUser(user);
    }

    /**
     * 更新用户信息
     *
     * @param user 用户
     * @return {@link Result}<{@link String}>
     */
    @PutMapping
    public Result<String> update(@RequestBody SysUser user) {
        return sysUserService.updateUserById(user);
    }

    /**
     * 删除
     *
     * @param id 用户id
     * @return {@link Result}<{@link String}>
     */
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable String id) {
        return sysUserService.deleteUserById(id);
    }
}
